//
//  TORecommendViewController.m
//  SeaGullFM
//
//  Created by dllo on 15/11/16.
//  Copyright © 2015年 tianou. All rights reserved.
//

#import "WSRecommendViewController.h"
#import "WSRecommendCell.h"
#import "WSResource.h"
#import "WSSectionModel.h"
#import "WSListModel.h"
#import "WSQualityCell.h"
#import "WSFindCell.h"
#import "WSSectionView.h"
#import "WSLunboView.h"
#import "WSAlbumsDetailViewController.h"
#import "WSRecommendDetailController.h"
#import "WSMoreViewController.h"
#import "WHCategoryScrollController.h"
#import "WSFindViewController.h"
#import "WSPlayViewController.h"


@interface WSRecommendViewController ()<UITableViewDelegate,UITableViewDataSource, WSRecommendCellDelegate, WSSectionViewDelegate, WSLunboViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *editorArray;
@property (nonatomic, strong) NSMutableArray *findArray;
@property (nonatomic, strong) NSMutableArray *discoveryArray;
@property (nonatomic, strong) NSMutableArray *hotArray;
@property (nonatomic, strong) NSMutableArray *hotRecommends;
@property (nonatomic, strong) NSMutableArray *sectionArray;
@property (nonatomic, strong) NSMutableArray *focusArray;
@property (nonatomic, strong) WSLunboView *headView;
@property(nonatomic,assign)NSInteger currentPage;

@end

@implementation WSRecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = WHITE;
    
    [self createTableView];
    [self getData];
    self.currentPage = 1;
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    self.tableView.mj_header = header;
}

- (void)createTableView {

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[WSRecommendCell class] forCellReuseIdentifier:@"recommendcell"];
    [self.tableView registerClass:[WSQualityCell class] forCellReuseIdentifier:@"qualitycell"];
    [self.tableView registerClass:[WSFindCell class] forCellReuseIdentifier:@"findcell"];
//    self.tableView.sectionHeaderHeight = 40;
    self.tableView.sectionFooterHeight = 10;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    self.headView = [[WSLunboView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH*0.45)];
    self.headView.delegate = self;
    self.tableView.tableHeaderView = self.headView;
    
}

#pragma mark - tableView dateSource & delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.sectionArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    WSListModel *model = [self.sectionArray objectAtIndex:section];
    
    WSSectionView *view = [[WSSectionView alloc] init];
    view.delegate = self;
    view.title = model.title;
    view.num = section;
    
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    WSListModel *model = [self.sectionArray objectAtIndex:section];
    
    if ([model.title isEqualToString:@"精品听单"] || [model.title isEqualToString:@"发现新奇"]) {
        return model.list.count;
    }

    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WSListModel *model = [self.sectionArray objectAtIndex:indexPath.section];
    
    if ([model.title isEqualToString:@"小编推荐"]) {
        
        WSRecommendCell *recommendCell = [tableView dequeueReusableCellWithIdentifier:@"recommendcell"];
        recommendCell.allPicArray = self.editorArray;
        recommendCell.title = model.title;
        recommendCell.delegate = self;
        return recommendCell;
    }
    else if ([model.title isEqualToString:@"精品听单"]) {
        
        WSQualityCell *qualityCell = [tableView dequeueReusableCellWithIdentifier:@"qualitycell"];
        if (indexPath.row == 0) {
            qualityCell.isShowLine = YES;
        }
        qualityCell.textModel = [self.findArray objectAtIndex:indexPath.row];
        return qualityCell;
    }
    else if ([model.title isEqualToString:@"发现新奇"]) {
        
        WSFindCell *findCell = [tableView dequeueReusableCellWithIdentifier:@"findcell"];
        if (indexPath.row < self.discoveryArray.count - 1) {
            findCell.isShowLine = YES;
        }
        findCell.model = [self.discoveryArray objectAtIndex:indexPath.row];
        return findCell;
    }
    else {
    
        WSRecommendCell *recommendCell = [tableView dequeueReusableCellWithIdentifier:@"recommendcell"];
        recommendCell.allPicArray = [self.hotArray objectAtIndex:indexPath.section - 3];
        recommendCell.title = model.title;
        recommendCell.delegate = self;
        return recommendCell;
    }
    
}

#pragma mark tableView点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    [self.headView.timer setFireDate: [NSDate distantFuture]];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WSListModel *model = [self.sectionArray objectAtIndex:indexPath.section];
    if ([model.title isEqualToString:@"精品听单"]) {
        
        WSRecommendDetailController *musicListVC = [[WSRecommendDetailController alloc] init];
        musicListVC.specialId = ((WSChildListModel *)model.list[indexPath.row]).specialId;
        [(WSTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:musicListVC];
        [self.navigationController pushViewController:musicListVC animated:YES];
        //        NSLog(@"%@", ((TOChildListModel *)model.list[indexPath.row]).specialId);
    }
    if ([model.title isEqualToString:@"发现新奇"]) {
        
        WSFindViewController *findVC = [[WSFindViewController alloc] init];
        
        if (indexPath.row == 2) {
            
            findVC.urlStr = @"http://www.wemart.cn";
        }
        else if (indexPath.row == 3) {
            
            findVC.urlStr = @"http://m.diyring.cc";
        }
//        shopVC.row = indexPath.row;
        [(WSTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:findVC];
        [self.navigationController pushViewController:findVC animated:YES];
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WSListModel *model = [self.sectionArray objectAtIndex:indexPath.section];
    if ([model.title isEqualToString:@"精品听单"]) {
        return SCREEN_WIDTH*0.25;
    }
    else if ([model.title isEqualToString:@"发现新奇"]) {
        return SCREEN_WIDTH*0.24;
    }
    
    return 170;
    
}

#pragma mark - 获取数据
-(void)getData
{

    [self.tableView.mj_header beginRefreshing];
    NSString *urlStr = @"http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=ios-b1&device=iPhone&includeActivity=true&includeSpecial=true&scale=2&version=4.3.8";
    
    [WSResource getDataWithURL:urlStr block:^(id result) {
        
        //初始化数组
        self.focusArray = [NSMutableArray array];
        self.editorArray = [NSMutableArray array];
        self.findArray = [NSMutableArray array];
        self.discoveryArray = [NSMutableArray array];
        self.hotArray = [NSMutableArray array];
        self.hotRecommends = [NSMutableArray array];
        
        //        NSLog(@"%@", result);
        WSSectionModel *section = [WSSectionModel modelWithDictionary:result];
        
        
        //轮播图数据
        [self.headView createSubViews:section.focusImage.list];
        self.focusArray = section.focusImage.list;
        self.editorArray = section.editorRecommendAlbum.list;
        self.findArray = section.specialColumns.list;
        self.discoveryArray = section.discoveryColumn.list;
        for (WSListModel *model in section.hotRecommend.list) {
            [self.hotRecommends addObject:model];
            [self.hotArray addObject:model.list];
        }
        //        NSLog(@"%@", self.hotArray);
        //区数组
        self.sectionArray = [NSMutableArray arrayWithObjects:section.editorRecommendAlbum,section.specialColumns,section.discoveryColumn, nil];
        [self.sectionArray addObjectsFromArray:section.hotRecommend.list];
        
        //刷新数据
        [self.tableView reloadData];
        self.currentPage++;
        
        [self.tableView.mj_header endRefreshing];
        
    } error:^(NSError *error) {
        
        [self.tableView.mj_header endRefreshing];
    }];
}


#pragma mark - 轮播图的跳转方法

-(void)getTapNumber:(NSInteger)number
{
    WSChildListModel *model =  [self.focusArray objectAtIndex:number];
    //type 2 专辑
    if (model.type == 2) {
        
        WSAlbumsDetailViewController *albumsDetailVC = [[WSAlbumsDetailViewController alloc] init];
        albumsDetailVC.navigationController.navigationBar.translucent = YES;
        [(WSTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:albumsDetailVC];
        albumsDetailVC.albumId = model.albumId;
        [self.navigationController pushViewController:albumsDetailVC animated:YES];
    }
    //type 3 音乐
    else if (model.type == 3)
    {
        WSPlayViewController *player = [WSPlayViewController sharaMusicPlayer];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:player];
        player.song_id = model.trackId;
        player.allMusicArray = [NSMutableArray arrayWithObjects:model, nil];
        player.indexRow = 0;
        [self presentViewController:naVC animated:YES completion:^{
            
        }];
        
    }
    //精品听单
    else if (model.type == 9)
    {
        WSAlbumsDetailViewController *musicListVC = [[WSAlbumsDetailViewController alloc] init];
        musicListVC.albumId = model.albumId;
//        [(WSAlbumsDetailViewController *)self.tabBarController isHiddenTabBar:YES withViewController:musicListVC];
        [self.navigationController pushViewController:musicListVC animated:YES];
    }

}


#pragma mark - picCell协议方法  跳转到专辑详情

-(void)JumpToAlbumView:(NSString *)albumId title:(NSString *)myTitle {
    
    WSAlbumsDetailViewController *albumsDetailVC = [[WSAlbumsDetailViewController alloc] init];
    
    [(WSTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:albumsDetailVC];
    albumsDetailVC.albumId = albumId;
    albumsDetailVC.myTitle = myTitle;
    [self.navigationController pushViewController:albumsDetailVC animated:YES];
}

#pragma mark push到moreViewController
- (void)pushMoreViewControllerWithTitle:(NSString *)title num:(NSInteger)num {

    if ([title isEqualToString:@"精品听单"] || [title isEqualToString:@"小编推荐"]) {
        
        WSMoreViewController *moreVC = [[WSMoreViewController alloc] init];
        if ([title isEqualToString:@"精品听单"]) {
            
            moreVC.moreUrl = @"http://mobile.ximalaya.com/m/subject_list?device=iphone&page=1&per_page=10&title=更多";
        }
        else if ([title isEqualToString:@"小编推荐"]) {
            
            moreVC.moreUrl = @"http://mobile.ximalaya.com/mobile/discovery/v1/recommend/editor?device=iphone&pageId=1&pageSize=20&title=更多";
        }
        moreVC.myTitle = title;
        [(WSTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:moreVC];
        [self.navigationController pushViewController:moreVC animated:YES];
    }
    else {
        
        WSListModel *model = [self.hotRecommends objectAtIndex:num - 3];
        WHCategoryScrollController *categorySVC = [[WHCategoryScrollController alloc] init];
        [(WSTabBarController*)self.tabBarController isHiddenTabBar:YES withViewController:categorySVC];
        categorySVC.moreTitleId = [NSString stringWithFormat:@"%@",model.categoryId];
        categorySVC.moreTitleS = model.title;
        [self.navigationController pushViewController:categorySVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
